import { Provide, Inject } from '@midwayjs/core';
import { UserDao } from '../dao/user.dao';
import { UpdateUserInfoDto } from '../dto/userDto';
import { User } from '../entity/User';

@Provide()
export class UserService {
    @Inject()
    userDao: UserDao;

    // 获取用户信息
    async getUserInfo(userId: string): Promise<{ success: boolean; message: string; data?: any }> {
        try {
            const user = await this.userDao.getUserById(userId);
            
            if (!user) {
                return {
                    success: false,
                    message: '用户不存在'
                };
            }
            return {
                success: true,
                message: '获取用户信息成功',
                data: {
                    id: user.id,
                    username: user.username,
                    name: user.name,
                    email: user.email,
                    phone: user.phone
                }
            };
        } catch (error) {
            console.error('获取用户信息失败:', error);
            return {
                success: false,
                message: '获取用户信息失败，请稍后重试'
            };
        }
    }

    // 更新用户信息
    async updateUserInfo(userId: string, updateDto: UpdateUserInfoDto): Promise<{ success: boolean; message: string; data?: any }> {
        try {
            const { email, phone } = updateDto;
            
            // 验证邮箱是否被其他用户使用
            if (email) {
                const emailExists = await this.userDao.checkEmailExistsForOtherUser(email, userId);
                if (emailExists) {
                    return {
                        success: false,
                        message: '该邮箱已被其他用户使用'
                    };
                }
            }

            // 验证手机号是否被其他用户使用
            if (phone) {
                const phoneExists = await this.userDao.checkPhoneExistsForOtherUser(phone, userId);
                if (phoneExists) {
                    return {
                        success: false,
                        message: '该手机号已被其他用户使用'
                    };
                }
            }

            // 构建更新数据
            const updateData: Partial<User> = {};
            if (email !== undefined) updateData.email = email;
            if (phone !== undefined) updateData.phone = phone;

            // 执行更新
            const updatedUser = await this.userDao.updateUserInfo(userId, updateData);
            
            if (!updatedUser) {
                return {
                    success: false,
                    message: '更新用户信息失败'
                };
            }

            console.log(`用户 ${userId} 信息更新成功`);
            
            return {
                success: true,
                message: '用户信息更新成功',
                data: {
                    id: updatedUser.id,
                    username: updatedUser.username,
                    name: updatedUser.name, 
                    email: updatedUser.email,
                    phone: updatedUser.phone
                }
            };
        } catch (error) {
            console.error('更新用户信息失败:', error);
            return {
                success: false,
                message: '更新用户信息失败，请稍后重试'
            };
        }
    }
}